package com.ruoyi.boat.controller;

import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.boat.aop.AvoidDuplicate;
import com.ruoyi.boat.domain.Path;
import com.ruoyi.boat.domain.PathPoint;
import com.ruoyi.boat.service.IPathService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.json.JSON;
import com.ruoyi.common.json.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 路径Controller
 * 
 * @author ruoyi
 * @date 2023-08-01
 */
@Controller
@RequestMapping("/boat/path")
public class PathController extends BaseController
{
    private String prefix = "boat/path";

    @Autowired
    private IPathService pathService;

    @RequiresPermissions("boat:path:view")
    @GetMapping()
    public String path()
    {
        return prefix + "/path";
    }

    /**
     * 查询路径列表
     */
    @RequiresPermissions("boat:path:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Path path)
    {
        startPage();
        List<Path> list = pathService.selectPathList(path);
        return getDataTable(list);
    }

    /**
     * 导出路径列表
     */
    @RequiresPermissions("boat:path:export")
    @Log(title = "路径", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @AvoidDuplicate
    @ResponseBody
    public AjaxResult export(Path path)
    {
        List<Path> list = pathService.selectPathList(path);
        ExcelUtil<Path> util = new ExcelUtil<Path>(Path.class);
        return util.exportExcel(list, "路径数据");
    }

    /**
     * 新增路径
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存路径
     */
    @RequiresPermissions("boat:path:add")
    @Log(title = "路径", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Path path)
    {
        if(StringUtils.isNotEmpty(path.getPathPointListJson())){
            List<PathPoint> pathPointList = null;
            try {
                pathPointList = JSON.unmarshalList(path.getPathPointListJson(), PathPoint.class);
                path.setPathPointList(pathPointList);
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.error();
            }
        }
        if(path.getId() != null){
            pathService.updatePath(path);
            return AjaxResult.success(pathService.selectPathById(path.getId()));
        }else{
            //新建航迹 status = 4
            path.setStatus(4);
            int rows = pathService.insertPath(path);
            if(rows > 0){
                path.setPathPointListJson("");//不需要传json字符串了
                return AjaxResult.success(path);
            }else{
                return AjaxResult.error();
            }
        }
    }

    /**
     * 修改路径
     */
    @RequiresPermissions("boat:path:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        Path path = pathService.selectPathById(id);
        mmap.put("path", path);
        return prefix + "/edit";
    }

    @RequiresPermissions("boat:path:edit")
    @GetMapping("/get/{pathId}")
    @ResponseBody
    public Path selectPath(@PathVariable("pathId") Integer pathId)
    {
        Path path = pathService.selectPathById(pathId);
        return path;
    }

    /**
     * 修改保存路径
     */
    @RequiresPermissions("boat:path:edit")
    @Log(title = "路径", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Path path)
    {
        return toAjax(pathService.updatePath(path));
    }

    /**
     * 删除路径
     */
    @RequiresPermissions("boat:path:remove")
    @Log(title = "路径", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(pathService.deletePathByIds(ids));
    }
}
